package Demo1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-07-07
 * Time: 16:56
 */
public class Test<T extends Number> {
    public Object[] arrays = new Object[10];

    //设置指定元素的数值
    public void setVal(int pos,T value){//假设pos位置都合法
        arrays[pos] = value;
    }

    //获取指定元素的数值
    public T getVal(int pos){//假设pos位置都合法
        return (T)arrays[pos];
    }

    public static void main(String[] args) {
        Test<Integer> test = new Test<Integer>();

    }
    public static void main4(String[] args) {
//        Test<String> test = new Test<String>();
//        test.setVal(0,"hello");
//
//        String str = test.getVal(0);//不需要向下转型，编译器已经帮我们实现类型转换了
    }
    public static void main3(String[] args) {
        Test<Integer> test = new Test();
        test.setVal(0,1);
//        test.setVal(1,"hello");
    }
    public static void main2(String[] args) {
        Test<String> test = new Test<String>();
        test.setVal(0,"hello");

        String str = test.getVal(0);
//      String str = (String)test.getVal(0); 需要向下装型
        System.out.println(str);
    }
    public static void main1(String[] args) {
        Test test = new Test();
        test.setVal(0,1);
        test.setVal(1,"hello");
        System.out.println(test.getVal(0));
        System.out.println(test.getVal(1));

        String str = (String)test.getVal(1);
        System.out.println(str);
    }
}
